package com.settlement.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.common.base.IBaseEnum;
import com.settlement.system.common.enums.AddressStatusEnum;
import com.settlement.system.common.util.StringUtils;
import com.settlement.system.mapper.SysAddressMapper;
import com.settlement.system.model.entity.SysAddress;
import com.settlement.system.model.form.SysAddressForm;
import com.settlement.system.model.query.SysAddressQuery;
import com.settlement.system.model.vo.SysAddressImportVo;
import com.settlement.system.service.SysAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 收货地址表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-07-23
 */
@Service
public class SysAddressServiceImpl extends ServiceImpl<SysAddressMapper, SysAddress> implements SysAddressService {

    @Override
    public Page<SysAddress> getPage(SysAddressQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysAddress> page = new Page<>(pageNum, pageSize);
        Page<SysAddress> mapperPage = this.baseMapper.getPage(page, queryParams);
        for (SysAddress record : mapperPage.getRecords()) {
            record.setUseStatusName(IBaseEnum.getLabelByValue(record.getUseStatus(), AddressStatusEnum.class));
        }
        return mapperPage;
    }

    @Override
    public boolean saveSysAddress(SysAddressForm sysAddressForm) {
        SysAddress sysAddress = new SysAddress();
        BeanUtils.copyProperties(sysAddressForm, sysAddress);
        sysAddress.setCreateTime(null);
        return this.saveOrUpdate(sysAddress);
    }

    @Override
    public SysAddressForm getSysAddressFormData(Long id) {
        SysAddress sysAddress = this.getById(id);
        SysAddressForm sysAddressForm = new SysAddressForm();
        BeanUtils.copyProperties(sysAddress, sysAddressForm);
        return sysAddressForm;
    }

    @Override
    public boolean deleteSysAddress(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysAddressImportVo> voList) {
        List<String> shortNameList = voList.stream().map(SysAddressImportVo::getShortName).distinct().toList();
        List<SysAddress> addressList = this.getBaseMapper().selectList(new LambdaQueryWrapper<SysAddress>().in(SysAddress::getShortName, shortNameList));
        Assert.isTrue(addressList.size() == 0, "系统已存在相同的地址简称:【%s】".formatted(String.join(",", addressList.stream().map(SysAddress::getShortName).toList())));
        List<SysAddress> sysAddressList = new ArrayList<>();
        for (SysAddressImportVo importVo : voList) {
            SysAddress sysAddress = new SysAddress();
            BeanUtils.copyProperties(importVo, sysAddress);
            sysAddressList.add(sysAddress);
        }
        this.saveOrUpdateBatch(sysAddressList);
    }

    @Override
    public List<SysAddressImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysAddress> sysAddressList = this.getBaseMapper().selectBatchIds(ids);
        List<SysAddressImportVo> voList = new ArrayList<>();
        for (SysAddress sysAddress : sysAddressList) {
            SysAddressImportVo vo = new SysAddressImportVo();
            BeanUtils.copyProperties(sysAddress, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public void changeStatus(Long id) {
        SysAddress sysAddress = this.getById(id);
        sysAddress.setUseStatus(AddressStatusEnum.NO_USE.getValue());
        this.updateById(sysAddress);
    }
}
